<template>
  <div class="signup-background">
    <div v-show="status" class="signup-vote-true">
      <VoteVue @submit="handleFormSubmit" />
    </div>
    <button @click="openJDProduct">跳转到京东商品</button>
    <div v-show="!status" class="signup-vote-flase">
      <button class="signup-vote-endTime-button">报名已结束</button>
      <p style="color: rgb(237, 110, 67); padding-top: 50px; fontsize: 13px">
        报名结束时间：{{ overdueTime }}
      </p>
    </div>
  </div>
</template>

<script setup>
import instance from "../../utils/http";
import { onMounted, ref } from "vue";
import newTime from "./data/time";
import dayjs from "dayjs";
import VoteVue from "./vote/vote.vue";

let overdueTime = ref("");
let errorMsg = ref("");
let status = ref(true);

const userData = ref(null);

const openJDProduct = () => {
  uni.navigateTo({
    url: "./web-view/web-view?url=https://item.jd.com/100124265215.html",
  });
};

const handleFormSubmit = async (data) => {
  userData.value = data;
  console.log("提交的数据", data);

  try {
    let UserObj = {
      name: data.name,
      cover: data.avatar,
      isApply: true,
      vote: 0,
      avtor: data.avatar,
      age: data.age,
      gender: data.gender === "男" ? true : false,
      voteNum: 0,
      position: data.position,
      resumeText: data.contact,
      visitNum: 0,
      richText: "",
      label: "",
    };

    await instance.post("http://1.92.114.25:6624/adduser", UserObj);
  } catch {
    console.error(err);
    uni.showToast({
      title: "请求失败，请稍后再试",
      icon: "none",
    });
  }
};

const overdue = async () => {
  try {
    let {
      data: { activityMsgs },
    } = await instance.get("https://zcgjcy.com/api/getactives");

    if (activityMsgs && activityMsgs.length > 0) {
      overdueTime.value = dayjs(activityMsgs[0].endTime).format(
        "YYYY-MM-DD HH:mm:ss"
      );
    } else {
      throw new Error("没有找到活动消息");
    }
  } catch (err) {
    console.error(err);

    errorMsg.value = "请求失败，请稍后再试";

    uni.showToast({
      title: "请求失败，请稍后再试",
      icon: "none",
    });
  }
};

const compareTime = async () => {
  if (overdueTime > newTime) {
    status.value = true;
  } else {
    status.value = false;
  }
};

onMounted(() => {
  overdue();
  compareTime();
});
</script>

<style scoped>
.signup-background {
  width: 100%;
  height: 155vh;
  background-color: rgb(51, 47, 56);
  color: white;
}

.error-message {
  color: red;
  font-size: 16px;
  margin-top: 10px;
}

.signup-vote-true {
  padding: 15px;
}

.signup-vote-flase {
  width: 300px;
  height: 150px;
  text-align: center;
  margin: 0 auto;
  padding-top: 230px;
}

.signup-vote-endTime-button {
  width: 150px;
  background-color: rgb(200, 35, 51);
  color: white;
}
</style>
